package com.neusoft.neu24.his.hisapp.controller;

import com.neusoft.neu24.his.hisapp.entity.Dept;
import com.neusoft.neu24.his.hisapp.service.IDeptService;
import com.neusoft.neu24.his.hisapp.service.impl.DeptServiceImpl;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.TextField;
import javafx.util.StringConverter;


public class DeptController {

    private IDeptService deptService =  new DeptServiceImpl();

//    在控制器中实现控件的属性绑定
    private  final Dept dept =new Dept(1,"内科","北京","123456");

    @FXML
    private TextField tf_deptno;
    @FXML
    private TextField tf_dname;
    @FXML
    private TextField tf_loc;
    @FXML
    private TextField tf_tel;


    private StringProperty dnameProperty =new SimpleStringProperty("abc");



//    在控制器启动过程中，需要自动完成初始化操作
    @FXML
    public void initialize() {
        tf_dname.textProperty().bindBidirectional( dept.getDnameProperty() );
        tf_loc.textProperty().bindBidirectional( dept.getLocProperty() );
        tf_tel.textProperty().bindBidirectional( dept.getTelProperty() );
        tf_deptno.textProperty().bindBidirectional(dept.getDeptnoProperty(), new StringConverter<Number>() {
            @Override
            public String toString(Number number) {

                return  number==null ? "": number.toString();
            }

            @Override
            public Number fromString(String string) {
                return  string ==null? null :  Integer.parseInt(string);
            }
        });

    }

    @FXML
    public void btnSaveAction(ActionEvent event) {
        try {
            deptService.addDept(dept);
        }
        catch ( Exception ex ){
            System.out.println(ex.getMessage());
        }

    }
}
